package com.songshuai.webapi.jobapi.service.impl;

import com.songshuai.webapi.jobapi.dao.crm.ClueInfoMapper;
import com.songshuai.webapi.jobapi.dao.crm.ListeninvitationMapper;
import com.songshuai.webapi.jobapi.dao.crmbase.TpoTeacherInfoMapper;
import com.songshuai.webapi.jobapi.dao.edu.TPOStudentShiTingInfoMapper;
import com.songshuai.webapi.jobapi.dao.edu.TpoStudentInfoMapper;
import com.songshuai.webapi.jobapi.dao.productcenter.YixueCourseMapper;
import com.songshuai.webapi.jobapi.model.crm.ClueInfo;
import com.songshuai.webapi.jobapi.model.crm.Listeninvitation;
import com.songshuai.webapi.jobapi.model.crmbase.TpoTeacherInfoWithBLOBs;
import com.songshuai.webapi.jobapi.model.edu.TpoStudentInfoWithBLOBs;
import com.songshuai.webapi.jobapi.model.edu.TpoStudentShitingInfo;
import com.songshuai.webapi.jobapi.model.productcenter.YixueCourse;
import com.songshuai.webapi.jobapi.service.ScheduleBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Auther: Administrator
 * @Date: 2018/11/2 0002 10:03
 * @Description:
 */
@Component
public class ScheduleBaseServiceImpl implements ScheduleBaseService {

    @Autowired
    TpoStudentInfoMapper studentInfoMapper;

    @Autowired
    YixueCourseMapper courseMapper;

    @Autowired
    ClueInfoMapper clueInfoMapper;

    @Autowired
    TPOStudentShiTingInfoMapper shiTingInfoMapper;

    @Autowired
    TpoTeacherInfoMapper teacherInfoMapper;

    @Autowired
    ListeninvitationMapper listeninvitationMapper;

    /**
     * 获取学生信息
     * @param studentGuids
     * @return
     */
    @Override
    public Map<String, TpoStudentInfoWithBLOBs> getStudentInfoMap(List<String> studentGuids) {
        Map<String,TpoStudentInfoWithBLOBs> retMap=new HashMap<>();
        if(studentGuids==null||studentGuids.size()==0){
            return retMap;
        }
        List<TpoStudentInfoWithBLOBs>studentInfos=studentInfoMapper.selectByStuGuids(studentGuids);
        if(studentInfos!=null){
            for(TpoStudentInfoWithBLOBs stu:studentInfos){
                retMap.put(stu.getGuid(),stu);
            }
        }
        return retMap;
    }

    /**
     * 获取课程信息
     * @param courseIds
     * @return
     */
    @Override
    public Map<Integer, YixueCourse> getCourseMap(List<Integer> courseIds) {
        Map<Integer, YixueCourse> retMap=new HashMap<>();
        if(courseIds==null||courseIds.size()==0){
            return retMap;
        }
        List<YixueCourse>courseList=courseMapper.getCourseByIds(courseIds);
        if(courseList!=null&&courseList.size()>0){
            for(YixueCourse course:courseList){
                retMap.put(course.getId(),course);
            }
        }
        return retMap;
    }

    /**
     * 获取线索信息
     * @param clueIds
     * @return
     */
    @Override
    public Map<String, ClueInfo> getClueInfosMap(List<String> clueIds) {
        Map<String,ClueInfo>retMap=new HashMap<>();
        if(clueIds==null||clueIds.size()==0){
            return  retMap;
        }
        List<ClueInfo>clueInfos= clueInfoMapper.selectByClueGuids(clueIds);
        if(clueInfos!=null&&clueInfos.size()>0){
            for(ClueInfo clue :clueInfos){
                retMap.put(String.valueOf(clue.getClueguid()),clue);
            }
        }
        return retMap;
    }

    /**
     * 获取试听学员信息
     * @param clueGuids
     * @return
     */
    @Override
    public Map<String, TpoStudentShitingInfo> ShiTingInfoMap(List<String> clueGuids) {
        Map<String,TpoStudentShitingInfo>retMap=new HashMap<>();
        if(clueGuids==null||clueGuids.size()==0){
            return retMap;
        }
        List<TpoStudentShitingInfo>shitingInfoList= shiTingInfoMapper.getStudentsByClueIds(clueGuids);
        if(shitingInfoList!=null&&shitingInfoList.size()>0){
            for(TpoStudentShitingInfo shitingInfo:shitingInfoList){
                retMap.put(shitingInfo.getClueGuid(),shitingInfo);
            }
        }
        return retMap;
    }

    /**
     *根据lessionid 获取试听学生信息
     * @param lessionIds
     * @return
     */
    @Override
    public Map<Integer, TpoStudentShitingInfo> ShiTingInfoByLessonIdMap(List<Integer> lessionIds) {
        Map<Integer,TpoStudentShitingInfo>retMap=new HashMap<>();
        if(lessionIds==null||lessionIds.size()==0){
            return retMap;
        }
        List<Listeninvitation>shitingInfoList= listeninvitationMapper.selectBylessonIds(lessionIds);
        if(shitingInfoList!=null&&shitingInfoList.size()>0){
            List<Long>clueGuids=shitingInfoList.stream().map(Listeninvitation::getClueguid).filter(f->f!=null&&f>0).distinct().collect(Collectors.toList());
            List<String>strClueGuids=new ArrayList<>();
            for(Long itm:clueGuids){
                strClueGuids.add(String.valueOf(itm));
            }
            Map<String,Integer>listenMap=new HashMap<>();
            for(Listeninvitation listeninvitation:shitingInfoList){
                if(listeninvitation.getClueguid()==null||listeninvitation.getClueguid()<=0){
                    continue;
                }
                listenMap.put(String.valueOf(listeninvitation.getClueguid()),listeninvitation.getLessonid());
            }
            List<TpoStudentShitingInfo> studentShitingInfos= shiTingInfoMapper.getStudentsByClueIds(strClueGuids);
            if(studentShitingInfos==null||studentShitingInfos.size()==0){
                return retMap;
            }
            for(TpoStudentShitingInfo shitingInfo:studentShitingInfos){
                Integer key=listenMap.getOrDefault(shitingInfo.getClueGuid(),0);
                if(key==null||key<=0){
                    continue;
                }
                retMap.put(key,shitingInfo);
            }
        }
        return retMap;
    }

    /**
     *获取教师信息
     * @param teacherIds
     * @return
     */
    @Override
    public Map<Integer, TpoTeacherInfoWithBLOBs>getBaseTeacherInfoMap(List<Integer>teacherIds){
        Map<Integer, TpoTeacherInfoWithBLOBs> retMap=new HashMap<>();
        if(teacherIds==null||teacherIds.size()==0){
            return retMap;
        }
        List<TpoTeacherInfoWithBLOBs>teachers= teacherInfoMapper.selectByIds(teacherIds);
        if(teachers==null||teachers.size()==0){
            return retMap;
        }
        for(TpoTeacherInfoWithBLOBs teacher:teachers){
            retMap.put(teacher.getUserid(),teacher);
        }
        return retMap;
    }
}
